package com.zoo.diannaopeijian.service.impl;

import com.zoo.diannaopeijian.domain.Entryitem;
import com.zoo.diannaopeijian.domain.ExportItem;
import com.zoo.diannaopeijian.domain.ExportRecord;
import com.zoo.diannaopeijian.domain.Stock;
import com.zoo.diannaopeijian.service.ExportItemService;
import com.zoo.diannaopeijian.service.ExportRecordService;
import com.zoo.diannaopeijian.service.ExportService;
import com.zoo.diannaopeijian.service.StockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zx583 on 2018-01-30.
 */
@Service
public class ExportServiceImpl implements ExportService {
    @Resource
    private ExportItemService exportItemService;
    @Resource
    private ExportRecordService exportRecordService;
    @Resource
    private StockService stockService;

    @Override
    public boolean exportWares(Integer staffid, List<ExportItem> exportItems) {
        // 创建出货记录
        ExportRecord exportRecord = new ExportRecord();
        exportRecord.setStaffid(staffid);
        exportRecord.setExportrecordtime(new Date());

        boolean result = exportRecordService.insertExportRecord(exportRecord);

        List<Stock> stocks = new ArrayList<>();
        // 插入出货记录项，同时创建库存对象
        for (ExportItem item : exportItems) {
            item.setExportrecordid(exportRecord.getExportrecordid());
            stocks.add(Stock.buildStock(item));
        }

        result = result && exportItemService.insertExportItems(exportItems);

        // 修改库存
        result = result && stockService.reduceStocks(stocks);

        if (!result) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return result;
    }

    @Transactional
    @Override
    public boolean ReexportWares(List<ExportItem> exportItems) {
        boolean result = true;

        // 删除原本的出货项
        ExportItem exportItem = new ExportItem();
        exportItem.setExportrecordid(exportItems.get(0).getExportrecordid());
        List<ExportItem> itemsBeDelete = exportItemService.deleteExportItems(exportItem);

        // 修改库存
        // 构造要修改的库存对象
        List<Stock> stocks = new ArrayList<>();
        for (ExportItem item : itemsBeDelete) {
            stocks.add(Stock.buildStock(item));
        }
        result = result && stockService.addStocks(stocks);

        // 插入出货记录项
        result = result && exportItemService.insertExportItems(exportItems);

        // 修改库存
        // 修改库存
        stocks.clear();
        for (ExportItem item : exportItems) {
            stocks.add(Stock.buildStock(item));
        }
        result = result && stockService.reduceStocks(stocks);

        if (!result) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return result;
    }
}
